package com.slipper.codeless.controller;

import com.slipper.constants.Constants;
import com.slipper.core.tree.TreeInfo;
import com.slipper.core.tree.TreeUtil;
import com.slipper.system.menu.bo.MenuBO;
import com.slipper.system.menu.service.MenuService;
import com.slipper.system.subsystem.bo.SubsystemBO;
import com.slipper.system.subsystem.service.SubsystemService;
import com.slipper.util.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/portal")
public class PortalController {
    @Resource
    private MenuService menuService;
    @Resource
    private SubsystemService subsystemService;

    @PostMapping("/menu/tree")
    public String queryPortalMenuTree() {
        SubsystemBO subsystemBO = new SubsystemBO();
        subsystemBO.setType(Constants.Subsystem.TYPE_PORTAL);
        subsystemBO.setValid(Constants.YES);
        List<SubsystemBO> subsystemList = subsystemService.queryList(subsystemBO);
        if (subsystemList.isEmpty()) return ResultUtil.failureResult("500", "门户端未启用！");

        MenuBO menuQO = new MenuBO();
        menuQO.setSubsystemId(subsystemList.getFirst().getId());
        List<MenuBO> menuList = this.menuService.queryList(menuQO);

        TreeInfo treeInfo = new TreeInfo("菜单树");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField("parentId");
        treeInfo.setTextField("name");
        treeInfo.setHrefField("vuePath");
        treeInfo.setSortField("ordinal");
        return ResultUtil.successResult(TreeUtil.buildTree(treeInfo, menuList));
    }
}
